package com.ww.idiom.source;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import android.content.Context;

import com.ww.core.util.DeviceUtil;
import com.ww.core.util.Logger;
import com.ww.core.util.StringUtils;
import com.ww.idiom.db.IdiomDbHelper;
import com.ww.idiom.entity.Idiom;

/**
 * @since 
 * @version 2013-12-5
 * @author wangwei
 */
public class IdiomManager {

	public static void getWordMore(Context context, String name) {
		if (DeviceUtil.checkNet(context)) {
			Idiom temp = getWordfromNet(name);
			new IdiomDbHelper(context).updateWord(temp);
		}
	}

	/**
	 * 从百度字典获取汉字或词语的解�?
	 * @param name 汉字
	 * @return Word
	 */
	private static Idiom getWordfromNet(String name) {
		Idiom w = new Idiom();
		Logger.info("name\n" +name);
		Document document = null;
		try {
			document = Jsoup.connect("http://dict.baidu.com/s?wd=" + StringUtils.toUtf8String(name)).timeout(8000)
					.get();
			if (document != null) {
				try {
				String baike = document.getElementById("cn-baike-mean").getElementsByTag("div").get(0).html();
				w.setBaike(baike);
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				String detailmean = document.getElementById("cn-detailmean").getElementsByTag("ul").html();
				try {
				String syn = document.getElementById("cn-syn-ant").getElementsByTag("ul").html();
				w.setSynAnt(syn);
				} catch (Exception e) {
					e.printStackTrace();
				}
				w.setDetailmean(detailmean);
				w.setName(name);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return w;
	}
}
